package com.bobo.mm.socket;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.bobo.mm.BuildConfig;
import com.bobo.mm.ClientConfig;
import com.bobo.mm.IService;
import com.bobo.mm.ISocketListener;

/**
 * socket 通信服务
 * Created by cocolove2 on 17/9/12.
 */

public class SocketService extends Service {
    private static final String TAG = "SocketService";
    private Handler mHandler = new Handler();
    private final IService.Stub mBind = new IService.Stub() {
        @Override
        public boolean sendByteMessage(String channelId, byte[] message) throws RemoteException {
            return SocketManager.getInstance().sendMessage(channelId, message);
        }

        @Override
        public boolean sendTextMessage(String channelId, String message) throws RemoteException {
            return SocketManager.getInstance().sendMessage(channelId, message);
        }

        @Override
        public void start(final ClientConfig config, final ISocketListener listener) throws RemoteException {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    SocketManager.getInstance().start(SocketService.this, config, listener);
                }
            });
        }

        @Override
        public void stop(String channelId) throws RemoteException {
            SocketManager.getInstance().stop(channelId);
        }

        @Override
        public boolean isConnected(String channelId) throws RemoteException {
            return SocketManager.getInstance().isConnected(channelId);
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "CSocket客户端服务被绑定：" + intent.getAction());
        return mBind;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "欢迎使用CSocket客户端,当前版本为:V" + BuildConfig.VERSION_NAME);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getAction() != null) {
            Log.i(TAG, "CSocket客户端服务启动:" + intent.getAction() + "#" + flags + "#" + startId);
        } else {
            Log.i(TAG, "CSocket客户端服务重启:" + flags + "#" + startId);
        }
        if (intent.getAction() == null) {
            String action = getPackageName() + ".action.service_restart";
            Log.i(TAG, "服务重启，发送重启广播:" + action);
            Intent restartIntent = new Intent(action);
            restartIntent.setPackage(getPackageName());
            sendBroadcast(restartIntent);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onServiceKilled(null);
    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        onServiceKilled(rootIntent);
    }

    /**
     * 服务被杀死后释放资源
     */
    private void onServiceKilled(Intent rootIntent) {
        SocketManager.getInstance().stopAll();
        Log.i(TAG, "CSocket客户端服务被关闭");
    }
}
